<template>
  <div>
    <!-- 面包屑功能-眉角导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 按钮和输入框 -->
      <el-row :gutter='10'>
        <el-col :span="8">
          <!-- clearable 可清空的选项 -->
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query"
                    @clear='clearQuery'>
            <el-button slot="append" class="el-icon-search" @click="clearQuery">
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addGood">添加商品 </el-button>
        </el-col>
      </el-row>
      <!-- table表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="800">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="170">
          <template slot-scope="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <!-- 作用域插槽 -->
          <template>
            <el-button type="primary" size="mini" icon="el-icon-edit">
            </el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[10, 20, 30,]" :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  mounted () {
    this.getGoodList();
  },
  data () {
    return {
      // 查询的对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      tableData: [],
      // 数据的总条数
      total: 0
    }
  },
  methods: {
    // 跳转到添加good的页面,view的路由的调准啊
    addGood () {
      this.$router.push('/goods/add')
    },
    // 仿的淘宝,可以改的
    clearQuery () {
      this.queryInfo.pagenum = 1;
      this.queryInfo.pagesize = 10;
      this.getGoodList()
    },
    async getGoodList () {
      const { data: { data, meta } } = await this.$axios.get('goods', { params: this.queryInfo })
      console.log(data, meta)
      // 出错的情况
      if (meta.status !== 200) {
        return this.$message.error("获取商品列表失败")
      }
      this.tableData = data.goods
      this.total = data.total
      this.$message.success("获取商品列表成功")
    },
    // 改变显示的数据量
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize;
      this.getGoodList()
    },
    // 改变页数
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage;
      this.getGoodList()
    },
  }
}
</script>

<style lang="css" scoped>
</style>